package com.ruoyi.canteen.mapper;

import java.util.List;
import com.ruoyi.canteen.domain.CanteenCp;
import com.ruoyi.canteen.domain.CanteenCpClass;
import com.ruoyi.canteen.domain.CanteenCpSku;
import com.ruoyi.canteen.domain.CanteenCpSpecial;
import com.ruoyi.canteen.vo.CanteenCpVo;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;

/**
 * 菜品Mapper接口
 * 
 * @author ruoyi
 * @date 2021-09-08
 */
public interface CanteenCpMapper 
{
    /**
     * 查询菜品
     * 
     * @param id 菜品主键
     * @return 菜品
     */
    public CanteenCp selectCanteenCpById(Long id);

    /**
     * 查询菜品列表
     * 
     * @param canteenCp 菜品
     * @return 菜品集合
     */
    public List<CanteenCp> selectCanteenCpList(CanteenCp canteenCp);


    public List<CanteenCp> selectCanteenCpList2(CanteenCp canteenCp);

    public List<CanteenCpClass> getCanteenCpClassById(Long id);

    public List<CanteenCpSpecial> getCanteenCpSpecialsById(Long id);

    /**
     * 新增菜品
     * 
     * @param canteenCp 菜品
     * @return 结果
     */
    public int insertCanteenCp(CanteenCp canteenCp);

    /**
     * 修改菜品
     * 
     * @param canteenCp 菜品
     * @return 结果
     */
    public int updateCanteenCp(CanteenCp canteenCp);

    /**
     * 删除菜品
     * 
     * @param id 菜品主键
     * @return 结果
     */
    public int deleteCanteenCpById(Long id);

    /**
     * 批量删除菜品
     * 
     * @param ids 需要删除的数据主键集合
     * @return 结果
     */
    public int deleteCanteenCpByIds(Long[] ids);

    /**
     * 查询菜品列表
     * @param cpClassId 菜品类型
     * @return
     */
    @Select("SELECT id,md_id,`name`,url,tag,remark,order_no,`status`,limit_num,sepc_list from canteen_cp " +
            "where status=0 and delete_flag=0 and id in " +
            "( " +
            "SELECT cp_id from canteen_cp_class_relation as a " +
            "where a.class_id = #{cpClassId} and cp_id is not NULL " +
            ") order by order_no ASC;")
    @Results({
            @Result(property = "id", column = "id"),
            @Result(property = "mdId", column = "md_id"),
            @Result(property = "name", column = "name"),
            @Result(property = "url", column = "url"),
            @Result(property = "tag", column = "tag"),
            @Result(property = "remark", column = "remark"),
            @Result(property = "orderNo", column = "order_no"),
            @Result(property = "sepcList", column = "sepc_list"),
    })
    public List<CanteenCp> selectCpListByClass(@Param("cpClassId") Integer cpClassId);



    /**
     * 查询菜品SKU列表
     * @param cpClassId 菜品类型
     * @return
     */
    @Select("SELECT c.id as cp_id ,md_id,`name`,url,tag,remark,order_no,`status`,limit_num,spec_list,price,stock,s.id as cp_sku_id " +
            "from canteen_cp as c left join " +
            "canteen_cp_special as s on c.id = s.cp_id " +
            "where c.id in ( " +
            "SELECT cp_id from canteen_cp_class_relation as a where a.class_id = #{cpClassId} and cp_id is not NULL ) " +
            "order by order_no ASC")
    @Results({
            @Result(property = "cpId", column = "cp_id"),
            @Result(property = "mdId", column = "md_id"),
            @Result(property = "name", column = "name"),
            @Result(property = "url", column = "url"),
            @Result(property = "tag", column = "tag"),
            @Result(property = "remark", column = "remark"),
            @Result(property = "orderNo", column = "order_no"),
            @Result(property = "status", column = "status"),
            @Result(property = "limitNum", column = "limit_num"),
            @Result(property = "specList", column = "spec_list"),
            @Result(property = "price", column = "price"),
            @Result(property = "stock", column = "stock"),
            @Result(property = "cpSkuId", column = "cp_sku_id")

    })
    public List<CanteenCpSku> selectCpSkuListByClass(@Param("cpClassId") Integer cpClassId);


    /**
     * 查询菜品VO列表
     * @param cpClassId 菜品类型
     * @return
     */
    @Select("SELECT c.id,md_id,`name`,url,tag,remark,order_no,`status`,limit_num,price,spec_list,is_single_spec,is_must from canteen_cp as c left join " +
            "(SELECT cp_id,min(price) as price from canteen_cp_special group by cp_id) as s " +
            "on c.id = s.cp_id " +
            "where c.status=0 and delete_flag=0 and c.id in ( SELECT cp_id from canteen_cp_class_relation as a where a.class_id = #{cpClassId} and cp_id is not NULL ) " +
            "order by order_no ASC")
    @Results({
            @Result(property = "cpId", column = "cp_id"),
            @Result(property = "mdId", column = "md_id"),
            @Result(property = "name", column = "name"),
            @Result(property = "url", column = "url"),
            @Result(property = "tag", column = "tag"),
            @Result(property = "remark", column = "remark"),
            @Result(property = "orderNo", column = "order_no"),
            @Result(property = "status", column = "status"),
            @Result(property = "limitNum", column = "limit_num"),
            @Result(property = "specList", column = "spec_list"),
            @Result(property = "price", column = "price"),
            @Result(property = "isSingleSpec", column = "is_single_spec"),
            @Result(property = "isMust", column = "is_must"),
    })
    public List<CanteenCpVo> selectCpVoListByClass(@Param("cpClassId") Integer cpClassId);

    /***
     * 查询当前菜品包含在几个门店中
     * @param cpIds 菜品ID
     * @return
     */
    @Select("SELECT COUNT(DISTINCT md_id) as mdCount FROM `canteen_cp` " +
            "where id in (#{cpIds})")
    public Integer selectMdCountByCps(String cpIds);


}
